package speed2010.reformulacao.useful;

import java.io.IOException;

import com.maxmind.geoip.LookupService;

//GeoIP - finds the country of the logged user from his IP
public class CountryLookupService {

	private static CountryLookupService instance;

	static {
		CountryLookupService.instance = new CountryLookupService();
	}

	private CountryLookupService() {
	}

	public static CountryLookupService getInstance() {
		return CountryLookupService.instance;
	}

	public String getCountryName(String ip) {

		String country = "";
		String sep = System.getProperty("file.separator");
		String dir = System.getProperty("user.dir");
		String pack = "libs";

		String dbfile = dir + sep + pack + sep + "GeoIP.dat";

		try {
			LookupService cl = new LookupService(dbfile,
					LookupService.GEOIP_MEMORY_CACHE);

			country = cl.getCountry(ip).getName();

			cl.close();

		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		return country;
	}
}
